package com.dg.gaming.server.data.mysql;

import java.sql.*;
import java.io.*;
import com.dg.gaming.api.common.*;
import nanoxml.XMLElement;
import com.dg.gaming.api.common.util.*;
import java.util.Enumeration;
import com.dg.gaming.api.common.msg.Msg;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class DBCurrentTable extends ADBObject {
    private static final String SQL_LOAD_FULL = "select CTID,NumberofPlayers ,GameID, MODELDATA, StartTime,CreatedPlayer from  currenttable where CTID=?";

    private static final String SQL_SAVE =
            "UPDATE currenttable set MODELDATA=? where CTID=?";

    public static final PrepareStatmentObject PSO_LOAD_FULL = new
            PrepareStatmentObject(SQL_LOAD_FULL);

    public static final PrepareStatmentObject PSO_SAVE = new
            PrepareStatmentObject(SQL_SAVE);


    private long CTID;
    private int NumberofPlayers;
    private long GameID;
    private byte[] MODELDATA;
    private Date StartTime;
    private long CreatedPlayer;


    public DBCurrentTable() {
    }

    /**
     * loadFull
     *
     * @param rs ResultSet
     * @throws Exception
     * @todo Implement this com.dg.gaming.server.data.mysql.ADBObject method
     */
    public void loadFull(ResultSet rs) throws Exception {
        setCTID(rs.getLong("CTID"));
        setNumberofPlayers(rs.getInt("NumberofPlayers"));
        setGameID(rs.getLong("GameID"));
        try {
            setMODELDATA(DBManager.loadBytes(rs.getBinaryStream("MODELDATA")));
        } catch (Exception ex) {
        }
        setStartTime(new Date(rs.getTimestamp("StartTime").getTime()));
        setCreatedPlayer(rs.getLong("CreatedPlayer"));
    }

    /**
     * loadSimple
     *
     * @param rs ResultSet
     * @throws Exception
     * @todo Implement this com.dg.gaming.server.data.mysql.ADBObject method
     */
    public void loadSimple(ResultSet rs) throws Exception {
        loadFull(rs);
    }


    public Table getTable(GameModel model) throws Exception {
        Table tb = null;
        if (MODELDATA != null && MODELDATA.length > 0) {
            String str = new String(MODELDATA, "UTF8");
            XMLElement elem = new XMLElement();
            elem.parseString(str);
            tb = new Table(elem);
            tb.setModel(model);
            Enumeration e = elem.enumerateChildren();
            while (e.hasMoreElements()) {
                XMLElement childMessage = (XMLElement) e.nextElement();
                if (childMessage.getName().equals(Msg.MODEL)) {
                    if (model != null) {
                        model.setState(childMessage);
                    }

                }
            }
        } else {
            User u = new User("", getCreatedPlayer(), 0, 0, 0, 0, 0);
            tb = new Table(getCTID() + "", u, true,
                           new DGPropertyHash());
        }
        return tb;
    }


    public boolean loadFull(long CTID) throws Exception {
        PreparedStatement ps = PSO_LOAD_FULL.getPrepareStmt();
        ResultSet r = null;
        try {
            ps.setLong(1, CTID); ;
            r = ps.executeQuery();
            if (r.next()) {
                loadFull(r);
                return true;
            } else {
                return false;
            }

        } finally {
            if (r != null) {
                r.close();
            }
        }
    }

    /**
     * save
     *
     * @param ps PreparedStatement
     * @throws Exception
     * @todo Implement this com.dg.gaming.server.data.mysql.ADBObject method
     */
    public void save(PreparedStatement ps) throws Exception {
        ps.setBinaryStream(1, new ByteArrayInputStream(getMODELDATA()));
        ps.setLong(2, getCTID());
    }

    public void save() throws Exception {
        PreparedStatement ps = PSO_SAVE.getPrepareStmt();
        InputStream bodyIn = new ByteArrayInputStream(MODELDATA);
        ps.setBinaryStream(1, bodyIn, MODELDATA.length);
        ps.setLong(2, getCTID());
        ps.execute();
        bodyIn.close();
    }

    public long getCreatedPlayer() {
        return CreatedPlayer;
    }

    public void setCreatedPlayer(long CreatedPlayer) {
        this.CreatedPlayer = CreatedPlayer;
    }

    public long getCTID() {
        return CTID;
    }

    public void setCTID(long CTID) {
        this.CTID = CTID;
    }

    public long getGameID() {
        return GameID;
    }

    public void setGameID(long GameID) {
        this.GameID = GameID;
    }

    public byte[] getMODELDATA() {
        return MODELDATA;
    }

    public void setMODELDATA(byte[] MODELDATA) {
        this.MODELDATA = MODELDATA;
    }

    public int getNumberofPlayers() {
        return NumberofPlayers;
    }

    public void setNumberofPlayers(int NumberofPlayers) {
        this.NumberofPlayers = NumberofPlayers;
    }

    public Date getStartTime() {
        return StartTime;
    }

    public void setStartTime(Date StartTime) {
        this.StartTime = StartTime;
    }
}
